import axios, { AxiosError, AxiosPromise, AxiosResponse } from 'axios'
import IUserManageHttpApi from '../interfaces/UserManageInterface'
const moduleBaseUrl = "/user_manage"
import ResponseErrorHandler from '../handlers/ResponseErrorHandler'
import ResponseSuccessHandler from '../handlers/ResponseSuccessHandler'
import { NAME_Of_TOKEN, Result } from '../interfaces/PublicInterfaces'
// process 并不是nodejs中的方法, 而是vite.config.ts中定义的全局变量
const baseURL = process.env.BaseURL
const axiosInstance = axios.create({
    baseURL: baseURL + moduleBaseUrl
})

// 请求前的处理函数
// axiosInstance.interceptors.request.use(confirm => {

//     const localToken = localStorage.getItem('token')
//     confirm.headers.set('token', localToken)

//     return confirm
// })

axiosInstance.interceptors.response.use(ResponseSuccessHandler,ResponseErrorHandler)

export const _userManageHttpApi: IUserManageHttpApi = {
    PostEmailCode: function (user_email: string):Promise<null> {
        return axiosInstance.post("/post_email_code")
        .then(res =>{
            return null
        })
    },

    PostMobileCode: function (user_tel: string):Promise<null> {
        return axiosInstance.post("/post_mobile_code")
        .then(res=>{
            return null
        })
    },

    UserRegisterByEmail: function (user_name: string, user_email: string, user_password: string, code: string): Promise<null> {
        return axiosInstance.post("/register_by_email")
        .then(res=>{
            return null
        })
    },

    UserRegisterByMobile: function (user_name: string, user_tel: string, user_password: string, code: string):Promise<null> {
        return axiosInstance.post("/register_by_mobile")
        .then(res=>{
            return null
        })
    },

    UserActivate: function (): Promise<string> {
        
        return axiosInstance({
            method:"post",
            headers:{
                token:localStorage.getItem(NAME_Of_TOKEN)
            }
        }).then(res=>{
            return res.data as Result<string>
        }).then(res=>{
            return res.data
        })
    },

    UserLogin: function (user_email: string, user_tel: string, user_password: string): Promise<null> {
        return axiosInstance.post("/login")
        .then(res=>{
            return null
        })
    },

    GetUserInfo: function (): Promise<null> {
        return axiosInstance.post("/personal_info")
        .then(res=>{
            return null
        })
    }
}


